package org.fas.utils
{
	public class FuDate
	{
		public static var isDst:Boolean = false;
		public function FuDate()
		{
		}
//		public static function tranDateFormat(_time:String='2008-08-08 08:08:08',toFormat:String='m/d/y h:i:s[2c]'):String{
////			trace(getDateByString(_time).toLocaleDateString());
//			return getFormatString(getDateByString(_time),toFormat);
//		}
		public static function getDateByString(_timeString:String = '2008-08-08 08:08:08'):Date
		{
			_timeString = _timeString.replace('[dst]','');
			var _date:Date = new Date();
			var _arr:Array = _timeString.split(' ');
			var _dateArr:Array = _arr[0].toString().split('-');
//			trace(_arr[0],_dateArr[0],_dateArr[1],_dateArr[2])
			_date.setFullYear(_dateArr[0],_dateArr[1]-1,_dateArr[2]);
			if(_arr.length>1){
				var _timeArr:Array = _arr[1].toString().split(':');
				_date.setHours(_timeArr[0],_timeArr[1],_timeArr[2]);
			}
			return _date;
		}
		public static function tranToString(_time:Date,_formatString:String='m/d/y h:i:s[2c]'):String{
			return getFormatString(_time,_formatString);
		}
		public static function getFormatString(_time:Date,_formatString:String='y-m-d h:i:s[2c]'):String
		{
			var _str:String = _formatString;
			var _tempTime:Date = new Date();
			if(FuDate.isDst){
				/*_tempTime.setTime(_time.getTime()+1000*60*60);
				_time = _tempTime;*/
				_str = _str.replace('[tz]','EDT');
			}else{
				_str = _str.replace('[tz]','EST');
			}
			//whether has front suffix
			var _2c:Boolean = false;
			var _h:int = _time.getHours();
			if(_str.indexOf('[12]')!=-1){
				_str = _str.replace('[12]','');
				_h = _h>12?_h-12:_h;
			}
			if(_str.indexOf('[2c]')!=-1){
				_str = _str.replace('[2c]','');
				_2c = true;
			}else{
				_2c = false;
			}
			_str = _str.replace('y',_time.getFullYear());
			if(_2c){
				_str = _str.replace('m',FuNumber.addLeadingZero(_time.getMonth()+1));
				_str = _str.replace('d',FuNumber.addLeadingZero(_time.getDate()));
				_str = _str.replace('h',FuNumber.addLeadingZero(_h));
				_str = _str.replace('i',FuNumber.addLeadingZero(_time.getMinutes()));
				_str = _str.replace('s',FuNumber.addLeadingZero(_time.getSeconds()));
			}else{
				_str = _str.replace('m',_time.getMonth()+1);
				_str = _str.replace('d',_time.getDate());
				_str = _str.replace('h',_h);
				_str = _str.replace('i',_time.getMinutes());
				_str = _str.replace('s',_time.getSeconds());
			}
			_str = _str.replace('x',getX(_time));
			_str = _str.replace('ap',getAPm(_time));
			return _str;
		}
		private static var xs:Array = ['MON','TUR','WED','THU','FRI','SAT','SUN'];
		private static function getX(_time:Date):String{
			var x:int = _time.day;
			return _time.toString().split(' ')[0].toString().toUpperCase();
//			return xs[x].toString();
		}
		private static function getAPm(_time:Date):String{
			if(_time.getHours()>12){
				return 'PM';
			}
			return 'AM';
		}
	}
}